<template>
  <div>
    <Card style="margin-bottom: 10px">
      <Form inline class="form-search">

        <FormItem>
          <Input v-model="keyword" placeholder="单位名称" style="width: 200px" />
        </FormItem>

        <FormItem>
          <Button type="primary" @click="
  page = 1;
SearchUnit();
          " icon="md-search">查询</Button>
        </FormItem>
        <FormItem>
          <Button type="primary" @click="AddData()" icon="md-add">新增</Button>
        </FormItem>
      </Form>
    </Card>
 <Table ref="table" style="margin-bottom: 10px" border :loading="loadingTable" :columns="tableTitle"
          :data="tableData" :height="tableHeight"></Table>
    <Modal :width="400" v-model="modalData" :title="tempData.Id > 0 ? '修改' : '新增'" :mask-closable="false"  draggable sticky >
      <Form ref="formValidate" :model="tempData" :label-width="80" :rules="ruleValidate">
        <FormItem label="名称" prop="Name">
          <Input v-model="tempData.Name" />
        </FormItem>
  <FormItem label="排序" prop="Sort">
          <InputNumber :min="0" v-model="tempData.Sort"></InputNumber>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" @click="modalData = false">取消</Button>
        <Button type="primary" :loading="loading" @click="SaveUnit">确定</Button>
      </div>
    </Modal>
  </div>
</template>

<script>

import { SearchUnit, DeleteUnit, SaveUnit } from '@/api/product'
// import the component
import Treeselect from '@riophae/vue-treeselect'
// import the styles
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { InitUnitTree } from '@/libs/tools'
export default {
  name: 'unit',
  components: {
    Treeselect
  },
  data () {
    return {
      keyword: '',
      ruleValidate: {

        Name: [
          {
            required: true,
            message: '不能为空',
            trigger: 'blur'
          }
        ]
      },
      loading: false,
      loadingTable: false,
      modalData: false,
      tempData: {
        Name: '',
        ParentId: 0
      },
      tableTitle: [
        {
          type: 'index',
          title: '序号',
          align: 'center',
          width: 100
        },
        {
          title: '操作',
          align: 'center',
          width: 200,
          render: (h, params) => {
            return h('div', [
              h(
                'Button',
                {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      var vm = this
                      vm.tempData = params.row
                      vm.modalData = true
                    }
                  }
                },
                '修改'
              ),
              h(
                'Button',
                {
                  props: {
                    type: 'error',
                    size: 'small'
                  },
                  on: {
                    click: () => {
                      this.DeleteUnit(params.row.Id)
                    }
                  }
                },
                '删除'
              )
            ])
          }
        },
        {
          key: 'Name',
          title: '名称',
          width: 250,
          align: 'center'
        },

        {
          key: ' ',
          title: ' '
        }
      ],
      tableData: [],
      treeData: []
    }
  },
  computed: {},
  methods: {
    InitUnitTree,
    SaveUnit () {
      let vm = this
      vm.$refs['formValidate'].validate((valid) => {
        if (valid) {
          vm.loading = true
          SaveUnit(vm.tempData).then((res) => {
            vm.loading = false

            const data = res.data
            if (data.StatusCode == 200) {
              vm.modalData = false
              vm.$Message.success({ title: '提示', content: '保存成功' })
              vm.SearchUnit()
            } else {
              vm.$Message.error({ title: '提示', content: data.Errors })
            }
          })
        }
      })
    },
    SearchUnit () {
      var reqData = {
        keyword: this.keyword
      }
      SearchUnit(reqData).then((res) => {
        var data = res.data
        if (data.StatusCode == 200) {
          this.tableData = data.Data
        } else {
          vm.$Notice.error({
            title: data.Errors
          })
        }
      })
    },

    DeleteUnit (id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定要删除吗？',
        onOk: () => {
          DeleteUnit(id).then((res) => {
            const data = res.data
            if (data.StatusCode == 200) {
              this.SearchUnit()
              this.GetTreeUnit()
              this.$Message.success({ title: '提示', content: '删除成功' })
            } else {
              this.$Message.error({ title: '提示', content: data.Errors })
            }
          })
        },
        onCancel: () => { }
      })
    },

    AddData () {
      var vm = this
      vm.modalData = true
      vm.tempData = {
        Name: '',
        ParentId: 0
      }
    },
    EditData (row) {
      var vm = this
      vm.tempData = row
      vm.modalData = true
    }
  },

  mounted () {
    this.SearchUnit()
  }
}
</script>

<style>
</style>
